package service;

import dao.UserDao;

import model.User;
import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserService {
    private UserDao userDao;

    public UserService() {
        this.userDao = new UserDao();
    }



    public User getUserById(int userId) {
        return userDao.getUserById(userId);
    }
    JDBCUtil jdbcUtil = new JDBCUtil();
    User user = null;

    ResultSet resultSet = null;


    //管理员登录
    public User managerLogin(String username, String password) {


        try {
            jdbcUtil = new JDBCUtil();
            String sql = "SELECT * FROM user WHERE username = ? AND password = ? AND user_type = 2";
            resultSet = jdbcUtil.prepareQuery(sql, username, password);

            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("user_id"));
                user.setUsername(resultSet.getString("username"));
                user.setEmail(resultSet.getString("email"));
                user.setPhone(resultSet.getString("phone"));
                user.setUser_type(resultSet.getInt("user_type"));
                user.setCreated_at(resultSet.getString("created_at"));
            }
        } catch (SQLException e) {
            e.printStackTrace(); // You can also use a logging framework
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (jdbcUtil != null) jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return user;
    }




    //买家登录
    public User buyerLogin(String username, String password) {

        User user =null;
        UserDao userDao=new UserDao();
        user= userDao.findUser( username, password);
        return user;
    }

    //卖家登录
    public User sellerLogin(String username, String password) {
        try {
            jdbcUtil = new JDBCUtil();
            String sql = "SELECT * FROM user WHERE username = ? AND password = ? AND user_type = 1";
            resultSet = jdbcUtil.prepareQuery(sql, username, password);

            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("user_id"));
                user.setUsername(resultSet.getString("username"));
                user.setEmail(resultSet.getString("email"));
                user.setPhone(resultSet.getString("phone"));
                user.setUser_type(resultSet.getInt("user_type"));
                user.setCreated_at(resultSet.getString("created_at"));
            }
        } catch (SQLException e) {
            e.printStackTrace(); // You can also use a logging framework
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (jdbcUtil != null) jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return user;

    }
    //
    //注册
    public boolean is_register(String username) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "select * from user where username=? ";
        ResultSet resultSet = jdbcUtil.prepareQuery(sql, username);

        try {

            if (resultSet.next()) {
                // 登录成功
                return true;
            } else {
                // 登录失败，账号不存在
                return false;
            }
        } catch (SQLException e) {
            // 处理异常
            e.printStackTrace();
            return false; // 或者抛出自定义异常
        } finally {
            // 关闭数据库资源
            jdbcUtil.closeConnection();
        }
    }

    public boolean registerUser(User user) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        boolean registrationSuccess=false;
        String username = user.getUsername();

        String sql = "INSERT INTO user (username, password, email, phone, user_type) VALUES (?, ?, ?, ?, ?)";

        registrationSuccess = jdbcUtil.paramsUpdate(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getUser_type());

        return registrationSuccess;




    }

    public boolean updateUser(User user) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "UPDATE user SET username = ?, password = ?, email = ?, phone = ?, user_type = ? WHERE user_id = ?";
        return jdbcUtil.paramsUpdate(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getUser_type(), user.getId());
    }

}
